local g = vim.g

local show_chat = function()
    if g.avante_chat == 0 then
        vim.cmd("AvanteChat")
        g.avante_chat = 1
    else
        g.avante_chat = 0
        vim.cmd("AvanteToggle")
    end
end

return {
    {
        "yetone/avante.nvim",
        event = "VeryLazy",
        lazy = false,
        --version = "0.0.27"
        version = false,
        opts = {
            provider = "openai",
            auto_suggestions_provider = nil,
            providers = {
                openai = {
                    endpoint = "https://api.deepseek.com/v1",
                    model = "deepseek-chat",
                    timeout = 5000,
                    extra_request_body = {
                        temperature = 0,
                        max_completion_tokens = 8192, -- Increase this to include reasoning tokens (for reasoning models)
                        reasoning_effort = "medium", -- low|medium|high, only used for reasoning models
                    },
                },
            },
            hints = {
                enabled = false,
            },
            mode = "legacy", --"agentic"
            behaviour = {
                auto_set_keymaps = false,
                auto_apply_diff_after_generation = false, -- Disable automatic diff application
                minimize_diff = false, -- Show all lines in the diff
            },
            diff = {
                autojump = false, -- Disable automatic jumping to diffs
                list_opener = "copen", -- Open the diff list without automatic actions
                override_timeoutlen = -1, -- Disable timeout override for diff mappings
            },
            windows = {
                width = 50,
                ask = {
                    start_insert = false,
                },
                chat = {
                    split = "vertical",
                    size = "50%",
                    border = "rounded",
                    start_insert = false,
                    focusable = true,
                    enter = true,  -- Automatically enter the chat window
                },
            },
        },
        keys = {
            {
                "<M-c>",function()
                    show_chat()
                end
                ,mode={'n'}
                ,desc = "Avante chat window toggle"
            },
            {
                "<M-n>",function()
                    vim.cmd("AvanteStop")
                end
                ,mode={'n'}
                ,desc = "Avante stop"
            },
            {
                "<leader>an",function()
                    vim.cmd("AvanteChatNew")
                end
                ,mode={'n'}
                ,desc = "Avante new chat"
            },
            {
                "<leader>aa",function()
                    require("avante.api").add_selected_file(vim.fn.expand("%:p"))
                end
                ,mode={'n'}
                ,desc = "Avante add current buffer to selected files"
            },
            {
                "<leader>ad",function()
                    require("avante.api").remove_selected_file(vim.fn.expand("%:p"))
                end
                ,mode={'n'}
                ,desc = "Avante remove current buffer from selected files"
            },
            {
                "<leader>ah",function()
                    require("avante.api").select_history()
                end
                ,mode={'n'}
                ,desc = "Avante select chat history"
            },
            {
                "<leader>ac",function()
                    vim.cmd("AvanteClear") -- Clear the chat history
                end
                ,mode={'n'}
                ,desc = "Avante clear chat history"
            },
        },
        build = "make",
        dependencies = {
            "nvim-treesitter/nvim-treesitter",
            "nvim-lua/plenary.nvim",
        },
    },
}
-- EOP
